package com.sh.data.engine.web.openapi.controller.workspace;

import com.sh.data.engine.application.model.dto.authority.RoleDTO;
import com.sh.data.engine.application.model.dto.base.PagerResponse;
import com.sh.data.engine.application.model.request.management.ProjectRoleCreateRequest;
import com.sh.data.engine.application.model.request.management.ProjectRoleUpdateRequest;
import com.sh.data.engine.application.service.authority.RoleApplicationService;
import com.sh.data.engine.common.annotation.permissionCheck.PermissionCheckMethod;
import com.sh.data.engine.web.webapi.controller.base.response.RestResponseEntity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.sh.data.engine.common.enumDefinition.PermissionCheckEnum.EDIT_ROLE;

/**
 * 工作空间角色
 *
 * @author: mengzheng.mhc
 * @date: 2024/11/27 17:45
 */
@Validated
@RestController
@RequestMapping("/openapi/project/role")
@Tag(name = "工作空间角色", description = "工作空间角色相关接口")
public class WorkspaceRoleController {

    @Autowired
    private RoleApplicationService roleApplicationService;

    @GetMapping("/list")
    @Operation(summary = "获取工作空间角色列表", description = "该接口用于查询工作空间角色列表")
    public RestResponseEntity<PagerResponse<RoleDTO>> listRoles(
        @Parameter(description = "角色名称")
        @RequestParam(required = false) String name,
        @Parameter(description = "分页页码", schema = @Schema(defaultValue = "1", minimum = "1"))
        @RequestParam(required = false, defaultValue = "1") Integer pageNum,
        @Parameter(description = "分页大小", schema = @Schema(defaultValue = "10", minimum = "1"))
        @RequestParam(required = false, defaultValue = "10") Integer pageSize
    ) {
        var result = roleApplicationService.list(pageNum, pageSize, name, null);
        return RestResponseEntity.success(result);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工作空间角色", description = "该接口用于删除工作空间角色")
    @PermissionCheckMethod(value = EDIT_ROLE)
    public RestResponseEntity<Boolean> deleteRole(
        @Parameter(description = "角色ID", schema = @Schema(requiredMode = Schema.RequiredMode.REQUIRED))
        @RequestParam(value = "roleId") Integer roleId
    ) {
        var result = roleApplicationService.delRole(roleId, true);
        return RestResponseEntity.success(result);
    }

    @PostMapping("/add")
    @Operation(summary = "添加工作空间角色", description = "该接口用于添加工作空间角色")
    public RestResponseEntity<Boolean> addRole(@RequestBody @Validated ProjectRoleCreateRequest request) {
        var result = roleApplicationService.addRole(request, false);
        return RestResponseEntity.success(result);
    }

    @PostMapping("/update")
    @Operation(summary = "更新工作空间角色", description = "该接口用于更新工作空间角色")
    public RestResponseEntity<Boolean> updateRole(@RequestBody @Validated ProjectRoleUpdateRequest request) {
        var result = roleApplicationService.editRole(request, false);
        return RestResponseEntity.success(result);
    }
}
